package com.exam.online.system.controller;

import com.exam.online.system.constant.StatusCode;
import com.exam.online.system.entity.Question;
import com.exam.online.system.entity.Role;
import com.exam.online.system.entity.Student;
import com.exam.online.system.entity.Teacher;
import com.exam.online.system.service.impl.CourseServiceImpl;
import com.exam.online.system.service.impl.QuestionServiceImpl;
import com.exam.online.system.service.impl.UserServiceImpl;
import com.exam.online.system.util.PageModel;
import com.exam.online.system.util.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.exam.online.system.constant.Constant.SERVICE_PATH_PUBLIC;

/**
 * @author zp
 * @program online-exam-system
 * @description
 * @create 2019-05-05 19:46
 **/
@RestController
@RequestMapping(SERVICE_PATH_PUBLIC)
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private QuestionServiceImpl questionService;
    @Autowired
    private CourseServiceImpl courseService;

    /**
     * 获取角色身份列表
     *
     * @return
     */
    @PostMapping("/roles")
    public List<Role> getRoles() {
        return userService.findRoles();
    }

    /**
     * 登录
     *
     * @param type
     * @param account
     * @param password
     * @return
     */

    @PostMapping("/login")
    public ResponseModel login(Integer type, String account, String password) {

        if (type == 1) {
            //管理员
            if (account.equals("admin") || password.equals("admin")) {
                return ResponseModel.buildFromStateCode(StatusCode.SUCCESS);
            }
        } else if (type == 2) {
            //教师
            Teacher teacher = userService.teacherLogin(account, password);
            if (teacher == null) {
                return ResponseModel.buildFromStateCode(StatusCode.FAILE);
            }
            return ResponseModel.buildFromStateCode(StatusCode.SUCCESS, teacher);
        } else {
            //学生
            Student student = userService.stuLoggin(account, password);
            if (student == null) {
                return ResponseModel.buildFromStateCode(StatusCode.FAILE);
            }
            return ResponseModel.buildFromStateCode(StatusCode.SUCCESS, student);
        }
        return ResponseModel.buildFromStateCode(StatusCode.FAILE);

    }

    /**
     * 注册学生
     *
     * @param account
     * @param password
     * @param sex
     * @param grade
     * @param name
     * @param stuNum
     * @return
     */
    @PostMapping("/registerStudent")
    public ResponseModel registerStudent(String account, String password,
                                         Integer sex, Integer grade, String name,
                                         Integer stuNum, String courseList) {

        boolean b = userService.selectBystuNum(stuNum);
        if (b) {
            return ResponseModel.build(StatusCode.FAILE.getCode(), "该学号已经存在");
        }

        Integer l1 = userService.registerStudent(account, password, sex, grade, name, stuNum, courseList);

        if (l1 > 0) {
            return ResponseModel.buildFromStateCode(StatusCode.SUCCESS);


        } else {
            return ResponseModel.buildFromStateCode(StatusCode.FAILE);
        }

    }

    /**
     * 注册老师
     *
     * @param account
     * @param password
     * @param courseId
     * @param name
     * @param sex
     * @return
     */
    @PostMapping("/registerTeacher")
    public ResponseModel registerTeacher(String account, String password,
                                         Integer courseId, String name, Integer sex) {
        Integer l1 = userService.registerTeacher(account, password, courseId, name, sex);
        if (l1 > 0) {
            return ResponseModel.buildFromStateCode(StatusCode.SUCCESS);

        } else {
            return ResponseModel.buildFromStateCode(StatusCode.FAILE);

        }

    }


    /**
     * 分页获取课程列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/getAllCourse")
    public ResponseModel<PageModel> getAllCourse(@RequestParam("pageNo") Integer pageNo,
                                                 @RequestParam("pageSize") Integer pageSize) {

        PageModel pageModel = userService.getAllCourse(pageNo, pageSize);
        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS, pageModel);

    }

    /**
     * 分页获取所有老师
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/getAllTeacher")
    public ResponseModel<PageModel> getAllTeacher(@RequestParam("pageNo") Integer pageNo,
                                                  @RequestParam("pageSize") Integer pageSize) {
        PageModel pageModel = userService.getAllTeacher(pageNo, pageSize);
        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS, pageModel);

    }


    /**
     * 添加题目
     *
     * @param type
     * @param title
     * @param optiona
     * @param optionb
     * @param optionc
     * @param optiond
     * @param answer
     * @param score
     * @param grade
     * @param courseId
     * @return
     */
    @PostMapping("/addQuestion")
    public ResponseModel addQuestion(Integer type, String title, String optiona, String optionb,
                                     String optionc, String optiond, String answer,
                                     Integer score, Integer grade, Integer courseId) {

        Integer l1 = questionService.addQuestion(type, title, optiona, optionb, optionc, optiond, answer, score, grade, courseId);
        if (l1 > 0) {
            return ResponseModel.buildFromStateCode(StatusCode.SUCCESS);

        } else {
            return ResponseModel.buildFromStateCode(StatusCode.FAILE);

        }
    }

    /**
     * 获取所有问题
     *
     * @param pageSize
     * @param pageNo
     * @return
     */
    @PostMapping("/getAllQuestion")
    public ResponseModel getAllQuestion(Integer pageSize, Integer pageNo) {

        PageModel allQuestion = questionService.getAllQuestion(pageSize, pageNo);

        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS, allQuestion);

    }

    /**
     * 添加课程
     *
     * @param courseName
     * @param grade
     * @return
     */
    @PostMapping("/addCourse")
    public ResponseModel addCourse(String courseName, Integer grade, Integer teacherId) {

        courseService.addCourse(courseName, grade);

        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS);

    }

    /**
     * 分页获取所有老师
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/getAllStudent")
    public ResponseModel<PageModel> getAllStudent(@RequestParam("pageNo") Integer pageNo,
                                                  @RequestParam("pageSize") Integer pageSize) {
        PageModel pageModel = userService.getAllStudent(pageNo, pageSize);
        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS, pageModel);

    }

    /**
     * 编辑老师信息
     *
     * @param teacherId
     * @param account
     * @param password
     * @return
     */
    @PostMapping("/editTeacher")
    public ResponseModel editTeacher(Integer teacherId, String account, String password, String name) {
        Integer l1 = userService.editTeacher(teacherId, account, password, name);
        if (l1 > 0) {
            return ResponseModel.buildFromStateCode(StatusCode.SUCCESS);

        } else {
            return ResponseModel.buildFromStateCode(StatusCode.FAILE);

        }
    }

    /**
     * 获取老师信息
     *
     * @param teacherId
     * @return
     */
    @PostMapping("/getTeacherById")
    public ResponseModel getTeacherById(Integer teacherId) {
        Teacher teacher = userService.getTeacherById(teacherId);
        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS, teacher);
    }

    /**
     * 删除老师
     *
     * @param teacherId
     * @return
     */
    @PostMapping("/deleteTeacher")
    public ResponseModel deleteTeacher(Integer teacherId) {
        Integer l1 = userService.deleteTeacher(teacherId);
        if (l1 > 0) {
            return ResponseModel.buildFromStateCode(StatusCode.SUCCESS);

        } else {
            return ResponseModel.buildFromStateCode(StatusCode.FAILE);

        }
    }

    /**
     * 删除学生
     *
     * @param stuId
     * @return
     */
    @PostMapping("/deleteStu")
    public ResponseModel deleteStu(Integer stuId) {
        Integer l1 = userService.deleteStu(stuId);
        if (l1 > 0) {
            return ResponseModel.buildFromStateCode(StatusCode.SUCCESS);

        } else {
            return ResponseModel.buildFromStateCode(StatusCode.FAILE);

        }
    }

    /**
     * 修改学生
     *
     * @param stuId
     * @return
     */
    @PostMapping("/editStu")
    public ResponseModel editStu(Integer stuId, String acount, String password, String name, Integer grade) {
        Integer l1 = userService.editStu(stuId, acount, password, name, grade);
        if (l1 > 0) {
            return ResponseModel.buildFromStateCode(StatusCode.SUCCESS);

        } else {
            return ResponseModel.buildFromStateCode(StatusCode.FAILE);

        }
    }

    /**
     * 获取学生信息
     *
     * @param stuId
     * @return
     */
    @PostMapping("/getStuById")
    public ResponseModel getStuById(@RequestParam("stuId") Integer stuId) {
        Student stuById = userService.getStuById(stuId);
        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS, stuById);
    }


}
